import absevnt from '../abseventsvc.js'
import axios from 'axios';
import {LGXEnums}  from '../../canvas/common/sysenum.js';


class DBClickUMLPackageSvc extends absevnt.IEventSvc {
    constructor() {
        super();
        this.umlPackageUUID = "";
        this.packegeName = "";
        this.saveFlag = false;
        this.parentID = "";
    }
    act(e) {
        console.log("DBClickUMLPackageSvc:xxx---------");
        if (e.xlink && e.xlink.length > 0) {
            this.post2Open(e.xlink);
            this.umlPackageUUID = e.xlink;
            this.packegeName = e.textContent;
        }
    }

    post2OpenFile(url, obj) {
        let that = this;
        axios.post(url, obj).then(res => {
            let respData = res.data;
            console.log(respData);
            let validResData = false;
            if (typeof respData === 'object' && respData !== null) {
                // 变量是一个非null对象
                validResData = true;
            }

            if (validResData) {
                window.mainctl.initGraphObjFromStream(res.data, true);

                let fileInfo = { uuid: that.umlPackageUUID, name: that.packegeName, parent: this.parentID, projectid: '', fileMode: 'uml' };
                window.gstore.commit("switchFile", fileInfo);
            }
            else {
                if (!this.saveFlag) {
                    that.post2CreateNewChartForUMLPackage(that.umlPackageUUID, that.packegeName);
                    this.saveFlag = true;
                }
            }
        }).catch(function (error) {
            console.log(error);
        })
    }

    post2Open(uuid) {
        var url = "api/graph/getfiledata";
        var postObj = { "uuid": uuid };
        this.post2OpenFile(url, postObj);
    }


    post2CreateNewChartForUMLPackage(umlPackageUUID, packegeName) {
        console.log("save file");
        var postObj = {};
        postObj["uuid"] = umlPackageUUID;
        postObj["data"] = this.getEmptyFileData();
        postObj["name"] = packegeName;
        postObj["maintype"] = "subfile";
        postObj["filetype"] = window.gstore.state.curentFile.fileMode;
        let pid = window.gstore.state.curentFile["uuid"];
        if (!pid || pid == "") {
            pid = "root";
        }
        postObj["parent"] = pid;
        this.parentID = pid;

        let projectid = window.gstore.state.curentFile["projectid"];
        if (!projectid || projectid == "") {
            projectid = "";
            postObj["maintype"] = "file";
        }
        postObj["projectid"] = projectid;
        postObj["folder"] = "root";
        postObj["iconURL"] = "";

        let that = this;
        axios.post(url, obj).then(res => {
            let respData = res.data;
            console.log(res.data);
            if (respData.ret) {
                that.post2Open(respData.uuid);
            }
        }).catch(function (error) {
            console.log(error);
        })
    }

    getEmptyFileData() {
        var graphFileObj = {};
        var array = [];
        graphFileObj["elements"] = array;
        var version = "v0.0.0.1";
        graphFileObj["version"] = version;
        graphFileObj["mapScale"] = 1;
        graphFileObj["fileKind"] = 0;
        graphFileObj["scaling"] = 1;
        graphFileObj["vleft"] = 0;
        graphFileObj["vtop"] = 1000;
        graphFileObj["vright"] = 760;
        graphFileObj["vbottom"] = 0;
        graphFileObj["seed"] = 1000;
        graphFileObj["backColor"] = 16777215;
        graphFileObj["fileMode"] = "uml";
        return graphFileObj;
    }
}

export default { DBClickUMLPackageSvc }